using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Musicman.Client.GUIComponents
{
    public partial class FancyProgress : UserControl
    {
        public FancyProgress()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
        }

        private long maxOveralValue = 100;
        private long overalValue = 0;
        private long maxSingleValue = 10;
        private long singleValue = 10;

        private float sweep = 0;
        private int singleLength;
        private int overalPercentage = 0;

        private string currentWorking;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            SolidBrush sb = new SolidBrush(Color.Orange);
            Pen pOveral = new Pen(sb);
            Pen pSingle = new Pen(sb);

            Rectangle overalRect = new Rectangle(20, 20, this.Width - 40, this.Height - 40);
            Rectangle singleRectFrame = new Rectangle(45, this.Height / 2 - 10, this.Width - 90, 20);
            Rectangle singleRect = new Rectangle(45, this.Height / 2 - 10, singleLength, 20);
            LinearGradientBrush lgb = new LinearGradientBrush(overalRect, Color.Orange, Color.Red, 350f);
            Pen oOveralGradient = new Pen(lgb);


            Font f = new Font("Arial", 15, FontStyle.Regular);
            Font f2 = new Font("Arial", 10, FontStyle.Regular);
            pSingle.Color = Color.Orange;
            g.DrawRectangle(pSingle, singleRectFrame);

            if (singleRect.Width != 0 && singleRect.Height != 0)
            {
                LinearGradientBrush lgb1 = new LinearGradientBrush(singleRect, Color.Orange, Color.WhiteSmoke, 90f);
                Pen pSingleGradient = new Pen(lgb1);
                g.FillRectangle(pSingleGradient.Brush, singleRect);
            }

            pSingle.Color = Color.Black;
            g.DrawString(this.currentWorking, f2, pSingle.Brush, singleRect.Left, singleRect.Top);
            pSingle.Color = Color.WhiteSmoke;
            g.DrawString(String.Format("{0} % completed", overalPercentage), f, pSingle.Brush, singleRect.Left, singleRect.Top - 25);
            pSingle.Color = Color.Orange;

            if (this.overalValue != 0)
            {
                oOveralGradient.Width = 20;
                g.DrawArc(oOveralGradient, overalRect, 89, sweep + 1);
                pOveral.Color = Color.DimGray;
                pOveral.Width = 20;
                for (int i = 0; i < 30; i++)
                {
                    g.DrawArc(pOveral, overalRect, i * 12 + 15, .5f);
                }
            }
        }

        public long MaxOveralValue
        {
            get { return maxOveralValue; }
            set
            {
                maxOveralValue = value;
                sweep = (maxOveralValue == 0) ? 0f : (float)overalValue / (float)maxOveralValue * 360f;
                Refresh();
            }
        }

        public long OveralValue
        {
            get { return this.overalValue; }
            set
            {
                this.overalValue = value;
                sweep = (maxOveralValue == 0) ? 0f : (float)overalValue / (float)maxOveralValue * 360f;
                overalPercentage = (maxOveralValue == 0) ? 0 : (int)((float)overalValue / (float)maxOveralValue * 100f);
                Refresh();
            }
        }

        public long MaxSingleValue
        {
            get { return maxSingleValue; }
            set
            {
                maxSingleValue = value;
                singleLength = (int)((float)singleValue / (float)maxSingleValue * (float)(this.Width - 90));
                overalPercentage = (int)((float)overalValue / (float)maxOveralValue * 100f);
                Refresh();
            }
        }

        public long SingleValue
        {
            get { return singleValue; }
            set
            {
                singleValue = value;
                singleLength = (int)((float)singleValue / (float)maxSingleValue * (float)(this.Width - 90));
                Refresh();
            }
        }

        public string CurrentWorking
        {
            get { return currentWorking; }
            set
            {
                if (value != null)
                {
                    currentWorking = (value.Length > 25) ? value.Substring(0, 22) + "..." : value;
                    Refresh();
                }
            }
        }
    }
}
